package com.woniuxy.yoga.business.job;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.woniuxy.yoga.business.dao.mapper.BusinessPaymentMapper;
import com.woniuxy.yoga.business.dao.mapper.BusinessStoreMapper;
import com.woniuxy.yoga.business.dao.model.BusinessPayment;
import com.woniuxy.yoga.business.dao.model.BusinessStore;
import com.woniuxy.yoga.business.model.exception.BusinessException;
import com.woniuxy.yoga.business.model.exception.BusinessExceptionCode;
import com.woniuxy.yoga.commons.core.statedata.BusinessState;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@Component
public class PaymentExpireJob {

    @Autowired
    private BusinessStoreMapper businessStoreMapper;
    @Autowired
    private BusinessPaymentMapper businessPaymentMapper;

    /**
     * 商家入驻时间到期处理方法
     */
    @XxlJob("paymentExpire")
    @Transactional
    public void expire() {
        System.out.println("商家入驻到期");
        String paymentId = XxlJobHelper.getJobParam();
        //修改门店的状态
        BusinessPayment businessPayment = businessPaymentMapper.selectById(paymentId);
        LambdaQueryWrapper<BusinessStore> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BusinessStore::getAccountId, businessPayment.getAccountId());
        BusinessStore businessStore = businessStoreMapper.selectOne(queryWrapper);
        if (businessStore == null) {
            throw new BusinessException(BusinessExceptionCode.BUSINESS_EXCEPTION_CODE_STORE_NOTEXITS);
        }
        LambdaUpdateWrapper<BusinessStore> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(BusinessStore::getStoreState, BusinessState.STORE_STATE_DISABLE).
                set(BusinessStore::getEnterMoneyState, BusinessState.ENTER_MONEY_STATE_NO_PAY)
                .set(BusinessStore::getEnterPayTime, null)
                .eq(BusinessStore::getStoreId, businessStore.getStoreId());
        businessStoreMapper.update(null, updateWrapper);
        //生成入驻缴费表
        BusinessPayment newBusinessPayment = new BusinessPayment();
        //雪花算法生成缴费ID
        newBusinessPayment.setPaymentId(new Snowflake(1, 1).nextIdStr());
        newBusinessPayment.setAccountId(businessPayment.getAccountId());
        newBusinessPayment.setPaymentMoney(new BigDecimal(BusinessState.ENTER_MONEY));
        newBusinessPayment.setCreateTime(DateUtil.format(new Date(System.currentTimeMillis()), "yyyy-MM-dd HH:mm:ss"));
        newBusinessPayment.setPaymentState(BusinessState.ENTER_MONEY_STATE_NO_PAY);
        businessPaymentMapper.insert(newBusinessPayment);
    }
}
